package com.atguigu.gmall.common.service;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.common.entity.GmallCorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.UUID;

@Component
public class RabbitService {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private RedisTemplate redisTemplate;
    //发消息
    public void sendMessage(String exchange ,String  routingKey , Object msg){
        //交换机的应答机石失败的时候 ，重新发消息的对象 （要求：交换机 ，RoutingKey ，消息）
        GmallCorrelationData gmallCorrelationData = new GmallCorrelationData();//自定义实体类 继承CorrelationData
        //交换机
        gmallCorrelationData.setExchange(exchange);
        gmallCorrelationData.setRoutingKey(routingKey);
        gmallCorrelationData.setMessage(msg);
        //将gmallCorrelationData 保存到redis中
        String uuid = UUID.randomUUID().toString();
        System.out.println("uuid = " + uuid);
        gmallCorrelationData.setId(uuid);
        redisTemplate.opsForHash().put("reTrySendMessage",uuid, JSONObject.toJSONString(gmallCorrelationData));
        //发消息
        rabbitTemplate.convertAndSend(exchange,routingKey,msg,gmallCorrelationData);
    }

    //发送延迟消息  基于死信
    public void sendDeadLetterMessage(String exchange ,String  routingKey , Object msg,int delayTime){
        //交换机的应答机石失败的时候 ，重新发消息的对象 （要求：交换机 ，RoutingKey ，消息）
        GmallCorrelationData gmallCorrelationData = new GmallCorrelationData();//自定义实体类 继承CorrelationData
        //交换机
        gmallCorrelationData.setExchange(exchange);
        gmallCorrelationData.setRoutingKey(routingKey);
        gmallCorrelationData.setMessage(msg);
        //将gmallCorrelationData 保存到redis中
        String uuid = UUID.randomUUID().toString();
        System.out.println("uuid = " + uuid);
        gmallCorrelationData.setId(uuid);

        //当前是延迟消息
        gmallCorrelationData.setDelay(true);
        gmallCorrelationData.setDelayTime(delayTime*1000);

        //1：将GmallCorrelationData保存到缓存中
        redisTemplate.opsForHash().put("reTrySendMessage",uuid, JSONObject.toJSONString(gmallCorrelationData));
        //发消息
        rabbitTemplate.convertAndSend(exchange,routingKey,msg,message -> {
            //设置TTL存活时间的长度 毫秒是单位
            message.getMessageProperties().setExpiration(String.valueOf(gmallCorrelationData.getDelayTime()));
            System.out.println("发送延迟消息基于死信的时间："+new Date());
            return message;
        },gmallCorrelationData);
    }
    //发送延迟消息  基于插件
    public void sendDelayMessage(String exchange,String routingKey,Object msg,int delayTime) {
        //交换机应答失败的时候 重新发消息的 对象（要求：交换机、RoutingKey 、消息）
        GmallCorrelationData gmallCorrelationData = new GmallCorrelationData();
        //交换机
        gmallCorrelationData.setExchange(exchange);
        gmallCorrelationData.setRoutingKey(routingKey);
        gmallCorrelationData.setMessage(msg);
        String uuid = UUID.randomUUID().toString();
        System.out.println("uuid:" + uuid);
        gmallCorrelationData.setId(uuid);

        //当前是延迟消息
        gmallCorrelationData.setDelay(true);
        //延迟多长时间
        gmallCorrelationData.setDelayTime(delayTime*1000);

        //1：将GmallCorrelationData保存到缓存中
        redisTemplate.opsForHash().put("reTrySendMessage",uuid,
                JSONObject.toJSONString(gmallCorrelationData));//
        //发消息
        rabbitTemplate.convertAndSend(
                exchange,routingKey,msg,message -> {
                    //设置TTL存活时间的长度   毫秒是单位
                    message.getMessageProperties().setDelay(gmallCorrelationData.getDelayTime());
                    System.out.println("发送延迟消息基于插件的时间：" + new Date());
                    return message;
                },gmallCorrelationData);
    }
}
